package com.imdroid.carac.activities;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.imdroid.carac.IMDroid;
import com.imdroid.carac.R;
import com.imdroid.carac.model.User;
import com.imdroid.carac.process.ProcessFactory;
import com.imdroid.carac.process.UserProcess;

public class Register3Activity extends BaseActivity implements OnClickListener {
	/**
	 * 车辆型号
	 */
	private EditText etVechile_model;
	/**
	 * 车牌号码
	 */
	private EditText etBoard_plate;
	/**
	 * 车牌号码
	 */
	private EditText etFrame_number;
	/**
	 * 发动机号码
	 */
	private EditText etEngine_number;
	/**
	 * 下一步按钮
	 */
	private Button btFinish;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register3);

		// 获取控件
		etVechile_model = (EditText) this
				.findViewById(R.id.my_et_reg3_vechile_model);
		etBoard_plate = (EditText) this
				.findViewById(R.id.my_et_reg3_board_plate);
		etFrame_number = (EditText) this
				.findViewById(R.id.my_et_reg3_frame_num);
		etEngine_number = (EditText) this
				.findViewById(R.id.my_et_reg3_engine_num);
		btFinish = (Button) this.findViewById(R.id.my_btn_reg3_finish);
		btFinish.setOnClickListener(this);

		refresh();
	}

	/**
	 * Refresh the information
	 */
	public void refresh() {
		etVechile_model.setText(IMDroid.getUser().getVechile_model());
		etBoard_plate.setText(IMDroid.getUser().getBoard_plate());
		etFrame_number.setText(IMDroid.getUser().getFrame_number());
		etEngine_number.setText(IMDroid.getUser().getEngine_number());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	public void onClick(View v) {
		// 保存到share preference
		save();

		// 这里上面的save无论成功与否，都会运行下来，所以就算唯一性校验了，在手机端也不会看到重复的提示
		// 所以逻辑需要改改，

		// 登记用户
		try {
			User user = IMDroid.getUser();
			IMDroid.register(this, user);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 启动相关服务
		// if (IMDroid.getUser().getUser_id() > 0) {
		// Intent i = new Intent(Intent.ACTION_RUN);
		// i.setClass(this, NotificationService.class);
		// this.startService(i);
		// this.finish();
		// }
	}

	/**
	 * Save the information to share preference
	 */
	private void save() {
		IMDroid.getUser()
				.setVechile_model(etVechile_model.getText().toString());
		IMDroid.getUser().setBoard_plate(etBoard_plate.getText().toString());
		IMDroid.getUser().setFrame_number(etFrame_number.getText().toString());
		IMDroid.getUser()
				.setEngine_number(etEngine_number.getText().toString());
		IMDroid.registerUser(IMDroid.getUser());
		// 更新到服务器
		new Thread() {
			public void run() {
				UserProcess process = (UserProcess) ProcessFactory
						.getProcess(UserProcess.class);
				process.postUser(IMDroid.getUser());
			}
		}.start();
	}
}
